package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.blankj.utilcode.util.SizeUtils
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.CommentDetailsEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.CommentDetailsContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.CommentDetailsPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_comment_details.*

/**
 * Created by JacobHHH on 2018/4/20.
 * 评论详情
 */
class CommentDetailsActivity : BaseMvpActivity<CommentDetailsContract.Model,CommentDetailsContract.View,CommentDetailsPresenter>(),CommentDetailsContract.View {

    private lateinit var mComment_id : String

    override fun createPresenter(): CommentDetailsPresenter = CommentDetailsPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_comment_details

    override fun initData() {
        fl_loading.visibility= View.VISIBLE
        mComment_id=intent.getStringExtra("comment_id")
        getPresenter().loadData(mComment_id)
    }

    override fun initEvent() {

    }

    override fun setData(entity: CommentDetailsEntity?) {
        fl_loading.visibility= View.GONE
        ImageLoader.loadToUrl(mContext,iv_avatar,Constants.getBaseUrl()+entity!!.user_avatar)
        tv_name.text=entity.user_name
        tv_time.text=resources.getText(R.string.tv_issue).toString()+" "+entity.created_at
        tv_comment.text=entity.content
        if ("0" == entity.is_reply){ //商家未回复
            tv_reply.text=resources.getString(R.string.shop_unreply)
            tv_reply_time.visibility=View.INVISIBLE
            tv_shop_comment.visibility=View.INVISIBLE
        }
        tv_reply_time.text=resources.getText(R.string.tv_reply).toString()+entity.reply_time
        tv_shop_comment.text=entity.reply_content
        if (entity.imgs.isEmpty()){
            ll_img_container.visibility=View.GONE
            return
        }
        if (entity.imgs.size==1){
            val imageView = ImageView(mContext)
            val layoutParams = LinearLayout.LayoutParams(SizeUtils.dp2px(80.0f)
                    , LinearLayout.LayoutParams.MATCH_PARENT)
            imageView.layoutParams=layoutParams
            imageView.scaleType=ImageView.ScaleType.CENTER_CROP
            ImageLoader.loadToUrl(mContext,imageView,Constants.getBaseUrl()+entity.imgs[0])
            ll_img_container.addView(imageView)
        } else {
            for (i in entity.imgs.indices){
                val containerParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
                        , SizeUtils.dp2px(80.0f))
                ll_img_container.layoutParams=containerParams
                val imageView = ImageView(mContext)
                val layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,1.0f)
                layoutParams.setMargins(5,0,5,0)
                imageView.layoutParams=layoutParams
                imageView.scaleType=ImageView.ScaleType.CENTER_CROP
                ImageLoader.loadToUrl(mContext,imageView,Constants.getBaseUrl()+entity.imgs[0])
                ll_img_container.addView(imageView)
            }
        }
    }

    override fun onDataFailure(err: String?) {

    }

    private var startY=0.0f
    private var moveY =0.0f
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event!!.action){
            MotionEvent.ACTION_DOWN->{
                startY=event.y
            }
            MotionEvent.ACTION_MOVE->{
                moveY= startY-event.y
            }
            MotionEvent.ACTION_UP->{
                if (moveY<-300){

                    setResult(Constants.ACTIVITY_FINISH_RESULT_CODE)
                    finish()
                    overridePendingTransition(R.anim.translate_in,R.anim.translate_out)
                    return true
                }
            }
        }
        return false
    }
}